<template>         
        <el-upload
            style="display: inline-block;"
            action
            :on-change="handleChange"
            :on-exceed="handleExceed"
            :on-remove="handleRemove"
            :file-list="fileListUpload"
            :limit="limitUpload"
            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
            :auto-upload="false"
        >
           <el-button icon="el-icon-upload">导入试题</el-button>
        </el-upload>
</template>



<script>
import XLSX from "xlsx";
import fileSave from "file-saver";


import Blob from '@/vendor/Blob.js'
import Export2Excel from '@/vendor/Export2Excel.js'

export default {
    data() {
        return {
            limitUpload: 1,
            columnName: ["qstStem", "answer", "opt1", "opt2", "opt3", "opt4", "difficult", "analysis"],
            tableData: [
                {'index':'0',"nickName": "沙滩搁浅我们的旧时光", "name": "小明"},
                {'index':'1',"nickName": "女人天生高贵", "name": "小红"},
                {'index':'2',"nickName": "海是彩色的灰尘", "name": "小兰"}
            ]
        };
    },
    methods: {

        //导出的方法
        exportExcel() {
            require.ensure([], () => {
                // const { export_json_to_excel } = require('../excel/Export2Excel');
                const { export_json_to_excel } = require('@/vendor/Export2Excel');
                
                const tHeader = ['序号', '昵称', '姓名'];
                // 上面设置Excel的表格第一行的标题
                const filterVal = ['index', 'nickName', 'name'];
                // 上面的index、nickName、name是tableData里对象的属性
                const list = this.tableData;  //把data里的tableData存到list
                const data = this.formatJson(filterVal, list);
                export_json_to_excel(tHeader, data, '列表excel');
            })
        },

        formatJson(filterVal, jsonData) {
            return jsonData.map(v => filterVal.map(j => v[j]))
        },


        importf(obj) {
            console.log('obj', obj)
            let _this = this;

            let inputDOM = this.$refs.inputer; // 通过DOM取文件数据
            console.log('inputDOM', inputDOM);
            this.file = event.currentTarget.files[0];
            var rABS = false; //是否将文件读取为二进制字符串
            var f = this.file;

            var reader = new FileReader(); //if (!FileReader.prototype.readAsBinaryString) {

            FileReader.prototype.readAsBinaryString = function(f) {
                var binary = "";

                var rABS = false; //是否将文件读取为二进制字符串

                var pt = this;

                var wb; //读取完成的数据

                var outdata;

                var reader = new FileReader();

                reader.onload = function(e) {
                    var bytes = new Uint8Array(reader.result);

                    var length = bytes.byteLength;

                    for (var i = 0; i < length; i++) {
                        binary += String.fromCharCode(bytes[i]);
                    }

                    var XLSX = require("xlsx");

                    if (rABS) {
                        wb = XLSX.read(btoa(fixdata(binary)), {
                            //手动转化

                            type: "base64"
                        });
                    } else {
                        wb = XLSX.read(binary, {
                            type: "binary"
                        });
                    } // outdata就是你想要的东西 excel导入的数据

                    outdata = XLSX.utils.sheet_to_json(
                        wb.Sheets[wb.SheetNames[0]]
                    ); // excel 数据再处理
                    let arr = [];

                    console.log("outdata", outdata);
                    outdata.map(v => {
                        let obj = {};

                        obj.columnName[0] = v.不知;

                        obj.name = v.姓名;

                        obj.department = v.部门;

                        obj.secondDepartment = v.二级部门;

                        obj.status = v.状态;

                        obj.id = v.id;

                        arr.push(obj);
                    });

                    _this.accountList = [...arr];

                    console.log("导入的数据》》》》", _this.accountList);

                    _this.reload();
                };

                reader.readAsArrayBuffer(f);
            };

            if (rABS) {
                reader.readAsArrayBuffer(f);
            } else {
                reader.readAsBinaryString(f);
            }
        },
        handleChange(_this) {
            console.log('>>>>>>>>>>>>>>>>>>>',_this)
            this.importf();
            // this.importf(_this)
            // 一导入就会触发的函数、
           /* if (this.fileTemp) {
                if (
                    this.fileTemp.type ==
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
                    this.fileTemp.type == "application/vnd.ms-excel"
                ) {
                    this.importfxx(this.fileTemp);
                } else {
                    this.$message({
                        type: "warning",
                        message: "附件格式错误，请删除后重新上传！"
                    });
                }
            } else {
                this.$message({
                    type: "warning",
                    message: "请上传附件！"
                });
            }*/
            // console.log("handleChange", args);
        },
        handleExceed(args) {
            console.log("handleExceed", args);
        },
        handleRemove(args) {
            console.log("handleRemove", args);
        },
        fileListUpload(args) {
            console.log("fileListUpload", args);
        }
    }
};
</script>

